<template>
  <div>
    <div class="title-box">
      <div>用户注册</div>
    </div>
    <el-form ref="postForm" :model="postForm" :rules="loginRules">

      <el-form-item prop="userName">
        <el-input v-model="postForm.userName" style="width: 100%" placeholder="邮箱账号" prefix-icon="el-icon-mobile" />
      </el-form-item>

      <el-form-item prop="realName">
        <el-input v-model="postForm.realName" style="width: 100%" placeholder="用户名" prefix-icon="el-icon-user" />
      </el-form-item>

      <el-form-item prop="password">
        <el-input v-model="postForm.password" show-password style="width: 100%" placeholder="密码" type="password"
          prefix-icon="el-icon-lock" />
      </el-form-item>
      <el-form-item prop="captchaValue">
        <div class="code">
          <el-input placeholder="验证码编号" v-model="postForm.captchaValue"></el-input>
          <el-button @click="getCode" type="primary" :disabled="!isUse">{{ codeText }}
          </el-button>
        </div>
      </el-form-item>

      <el-form-item>
        <el-button :loading="loading" type="primary" style="width: 100%" @click.native.prevent="handleReg">注册</el-button>
      </el-form-item>

    </el-form>

    <div style="display: flex; align-items: center; justify-content: flex-end">
      <el-link type="primary" href="/#/login">已有账号</el-link>
    </div>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getRegisterCode } from '@/api/user'
export default {
  data() {
    return {
      postForm: {
        userName: '',
        realName: '',
        password: '',
        captchaValue: '',
      },
      isUse: true,
      codeText: '获取验证码',
      loginRules: {
        password: [{ required: true, trigger: 'blur', message: '登录密码不能为空！' }],
        userName: [{ required: true, trigger: 'blur', message: '用户名不能为空！' }],
        realName: [{ required: true, trigger: 'blur', message: '姓名不能为空！' }],
        captchaValue: [{ required: true, trigger: 'blur', message: '验证码不能为空' }]
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters([
      'siteData'
    ])
  },

  methods: {

    handleReg() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/reg', this.postForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/admin/dashboard' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    getCode() {
      let r = new RegExp('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$');
      if (!r.test(this.postForm.userName)) {
        this.$message.error('邮箱格式不正确');
      } else {
        getRegisterCode(this.postForm.userName)
          .then(res => {
            this.isUse = false;
            this.codeText = 60;
            let timer = setInterval(() => {
              if (this.codeText <= 60 && this.codeText > 0) {

                this.codeText--;
              } else {
                this.isUse = true;
                this.codeText = '获取验证码'
                clearInterval(timer);
              }
            }, 1000)
          })
          .catch(err => {

          })
      }

    },

  }
}
</script>
<style>
.title-box {
  margin-bottom: 10px;
}

.code {
  display: flex;
  height: 36px;
}

.code .el-input {
  width: 200px;
  height: 100%;
}

.code .el-button {
  width: 150px;
  margin-left: 20px;
}
</style>

